﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using eProject_SEM3_SecurityServices.ObjectClass;
using System.Data.SqlClient;
using System.Data;

namespace eProject_SEM3_SecurityServices.DataAccess
{
    public class DContractDetail
    {
        public bool AddNewContractDetail(OContractDetail contractdetail)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {                
               new SqlParameter("@contractId",contractdetail.ContractId),
               new SqlParameter("@startDay",contractdetail.StartDay),
               new SqlParameter("@endDay",contractdetail.EndDay),
               new SqlParameter("@customerId",contractdetail.CustomerId),
               new SqlParameter("@numberOfEmployee",contractdetail.NumberEmployee),
               new SqlParameter("@registryDay",contractdetail.RegistryDay),
               new SqlParameter("@serviceId",contractdetail.ServiceId),
               new SqlParameter("@status",contractdetail.Status)
            };

            return DConnectSQL.ExecuteNonQuery("AddNewContractDetail", CommandType.StoredProcedure, parameters);
        }
        public List<OContractDetail> getContractDetailList()
        {
            List<OContractDetail> listcontractdetail = null;
            using (DataTable table = DConnectSQL.ExecuteSelectCommand("GetContractDetailList",CommandType.StoredProcedure))
            {
                if (table.Rows.Count > 0)
                {
                    listcontractdetail = new List<OContractDetail>();

                    foreach (DataRow row in table.Rows)
                    {
                        OContractDetail contractdetail = new OContractDetail();
                        contractdetail.ContractId = Convert.ToInt32(row["contractId"]);
                        contractdetail.CustomerId = Convert.ToInt32(row["customerId"]);
                        contractdetail.NumberEmployee = Convert.ToInt32(row["numberEmployee"]);
                        contractdetail.StartDay = row["startDay"].ToString();
                        contractdetail.EndDay = row["endDay"].ToString();
                        contractdetail.RegistryDay = row["RegistryDay"].ToString();
                        contractdetail.ServiceId = int.Parse(row["serviceId"].ToString());
                        if (row["Status"].Equals(false))
                        {
                            contractdetail.Status = "Pending";
                        }
                        else
                        {
                            contractdetail.Status = "Seen";
                        }

                        listcontractdetail.Add(contractdetail);
                    }
                }
            }


            return listcontractdetail;
        }

        public OContractDetail getContractDetails(int contractID)
        {
            OContractDetail contractDetail = null;
            SqlParameter[] paramenters = new SqlParameter[]
            {
                new SqlParameter("@contractId",contractID)
            };

            using (DataTable table = DConnectSQL.ExecuteParamerizedSelectCommand("getcontractDetail", CommandType.StoredProcedure, paramenters))
            {
                if (table.Rows.Count == 1)
                {
                    DataRow row = table.Rows[0];

                    contractDetail = new OContractDetail();

                    contractDetail.ContractId = Convert.ToInt32(row["contractId"]);
                    contractDetail.RegistryDay = row["registryDay"].ToString();
                    contractDetail.ServiceId = Convert.ToInt32(row["serviceId"]);
                    contractDetail.StartDay = row["startDay"].ToString();
                    contractDetail.EndDay = row["endDay"].ToString();
                    contractDetail.NumberEmployee = Convert.ToInt32(row["numberEmployee"]);
                    contractDetail.ServiceId = int.Parse(row["serviceId"].ToString());
                    if ((bool)row["Status"] == true)
                    {
                        contractDetail.Status = "Seen";
                    }
                    else contractDetail.Status = "Pending";
                }
            }
            return contractDetail;
        }
        public bool deleteContractDetail(int id)
        {
             SqlParameter [] para = new SqlParameter[]
             {
                 new SqlParameter("@contractId",id)
             };
             return DConnectSQL.ExecuteNonQuery("DeleteContractDetail", CommandType.StoredProcedure, para); 
        }
        public bool updateContractDetail(OContractDetail oContractDetail)
        {
            int status = 0;
            try
            {
                status =int.Parse(oContractDetail.Status);
            }
            catch (Exception)
            {
                
                throw;
            }
            SqlParameter[] paramenters = new SqlParameter[]
            {
                new SqlParameter("@contractId",oContractDetail.ContractId),
                new SqlParameter("@startDay",oContractDetail.StartDay),
                new SqlParameter("@endDay",oContractDetail.EndDay),
                new SqlParameter("@registryDay",oContractDetail.RegistryDay),
                new SqlParameter("@numberEmployee",oContractDetail.NumberEmployee),
                new SqlParameter("@status",status)              
            };

            return DConnectSQL.ExecuteNonQuery("UpdateContractDetail",CommandType.StoredProcedure,paramenters);
        }
    }
}